package com.honeycomb.utils;

import java.security.Key;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;

import org.apache.commons.codec.binary.Base64;

public class Base64Utils {

	/*
	 * @content:内容
	 * @password:自定义的秘钥
	 * @type:加密还是解密
	 * @考虑properties添加key
	 * */
	public static final String idHandle(String content, String password, String type) {
		KeyGenerator generator = null;
		SecureRandom secureRandom = null;
		try {
			generator = KeyGenerator.getInstance("AES");
			secureRandom = SecureRandom.getInstance("SHA1PRNG" );  
		}
		catch(Exception ex) {
			throw new IllegalStateException("Exception occurs when init KeyGenerator and secureRandom.", ex);
		}
		
        secureRandom.setSeed(password.getBytes()); 
		generator.init(secureRandom);
		Key key = generator.generateKey();

		if (type.equals(SystemConstants.DECIPHERING)) {
			return getDesString(content, key);
		} 
		if (type.equals(SystemConstants.ENCIPHERMENT)) {
			return getEncString(content, key);
		}

		return null;
	}
	
	/*
	 * @strMing:输入的明文,
	 * @key:解密用的key
	 * @return 密文
	 * */
	private static String getEncString(String strMing,Key key) {
		byte[] byteMi = null;
		byte[] byteMing = null;
		String strMi = "";
		try {
			byteMing = strMing.getBytes("UTF8");
		}
		catch(Exception ex) {
			throw new IllegalStateException("Exception occurs when get getBytes of string " + strMing, ex);
		}
		
		byteMi = getEncCode(byteMing,key);
		strMi = Base64.encodeBase64URLSafeString(byteMi);

		return strMi;
	}
	
	private static byte[] getEncCode(byte[] byteS,Key key) {
		byte[] byteFina = null;
		Cipher cipher;
		try {
			cipher = Cipher.getInstance("AES");
			cipher.init(Cipher.ENCRYPT_MODE, key);
			byteFina = cipher.doFinal(byteS);
		} catch (Exception ex) {
			throw new IllegalStateException("Exception occurs when Cipher working.", ex);
		}
		return byteFina;
	}
	
	/*
	 * @strMi:密文
	 * @key:秘钥
	 * @return 明文
	 * */
	private static String getDesString(String strMi, Key key) {
		byte[] byteMing = null;
		byte[] byteMi = null;
		String strMing = "";
	
			byteMi = Base64.decodeBase64(strMi);
			byteMing = getDesCode(byteMi,key);
		try {
			strMing = new String(byteMing, "UTF8");
		} catch (Exception ex) {
			throw new IllegalStateException("Exception occurs when get create string from byte in UTF8.", ex);
		}
		return strMing;
	}
	
	private static byte[] getDesCode(byte[] byteD,Key key) {
		Cipher cipher;
		byte[] byteFina = null;
		try {
			cipher = Cipher.getInstance("AES");
			cipher.init(Cipher.DECRYPT_MODE, key);
			byteFina = cipher.doFinal(byteD);
		} catch (Exception ex) {
			throw new IllegalStateException("Exception occurs when Cipher working.", ex);
		}
		return byteFina;
	}
	
	
	public static void main(String[] args) {
		
		String out1 = idHandle("111", "cv", SystemConstants.ENCIPHERMENT);
		String out2 = idHandle("222", "cv", SystemConstants.ENCIPHERMENT);
		String out3 = idHandle("333", "cv", SystemConstants.ENCIPHERMENT);
		String out4 = idHandle("444", "cv", SystemConstants.ENCIPHERMENT);
		String out5 = idHandle("555", "cv", SystemConstants.ENCIPHERMENT);
		
		
		System.out.println(out1);
		System.out.println(out2);
		System.out.println(out3);
		System.out.println(out4);
		System.out.println(out5);
		
		System.out.println(idHandle(out1, "cv", SystemConstants.DECIPHERING));
		System.out.println(idHandle(out2, "cv", SystemConstants.DECIPHERING));
		System.out.println(idHandle(out3, "cv", SystemConstants.DECIPHERING));
		System.out.println(idHandle(out4, "cv", SystemConstants.DECIPHERING));
		System.out.println(idHandle(out5, "cv", SystemConstants.DECIPHERING));
	}
	
}
